<?php

class Product{
	
	//Public variable to be used in smarty template
	public $mProducts;
	public $mProductLocations;
	public $mLinkToContinueShopping;
	public $mLocations;
	
	//Private stuff
	public $_mProductId;
	
	public function __construct(){
		if(isset($_GET['ProductId'])){
			$this->_mProductId =(int)$_GET['ProductId'];
		}else{
			trigger_error('Product Id Not set ');
		}
	}	
		
	public function init(){
		
		//Get product details from business tier 
		$this->mProduct =Catalog::GetProductDetails($this->_mProductId );
		
		if(isset($_SESSION['link_to_continue_shopping'])){
			
			$continue_shopping =Link::QueryStringToArray($_SESSION['link_to_continue_shopping']);
			 
			$page =1;
			
			if(isset($continue_shopping['Page']))
			  $page=$continue_shopping['Page'];
			  
		

			if(isset($continue_shopping['CategoryId'])){
				$this->mLinkToContinueShopping =Link::ToCategory($continue_shopping['DepartmentId'],
				$continue_shopping['CategoryId'],$page);
			}elseif(isset($continue_shopping['DepartmentId'])){
				$this->mLinkToContinueShopping =Link::ToDepartment($continue_shopping['DepartmentId'],
				$page);
			}elseif(isset($continue_shopping['SearchResults'])){
				$this->mLinkToContinueShopping =Link::ToSearchResults(trim(str_replace('-', '', $continue_shopping['SearchString'])),
				 $continue_shopping['AllWords'], $page);
			}else{
				$this->mLinkToContinueShopping =Link::ToIndex($page);
			}
			
		  }	
			if($this->mProduct['image']){
				$this->mProduct['image'] =Link::Build('images/' .$this->mProduct['image']);
			}
		   if($this->mProduct['image_2']){
				$this->mProduct['image_2'] =Link::Build('images/' .$this->mProduct['image_2']);
			}
			
			
	      
	      $this->mProduct['attributes']=Catalog::GetProductAttributes($this->mProduct['product_id']) ;
	      $this->mLocations =Catalog::GetProductLocations($this->_mProductId) ;
	      $this->mProduct['link_to_add_product']= Link::ToAddProduct($this->_mProductId);
	      
	      // craete add cart  
	      
	      for($i=0 ;$i < count($this->mLocations);$i++){
	      	$this->mLocations[$i]['link_to_department']=
	      	Link::ToDepartment($this->mLocations[$i]['department_id']);
	      	
	      	$this->mLocations[$i]['link_to_category']=
	      	Link::ToDepartment($this->mLocations[$i]['department_id'],$this->mLocations[$i]['category_id']);
	      	
	      }
	
	}
	
	
}
